/*Statement - Given a gold mine of n*m dimensions. Each field in this mine contains a positive integer
 which is the amount of gold in tons. Initially the miner is at first column but can be at any row.
  the miner can move to the cell diagonally up towards the right or right or diagonally down towards the right.
  Find out maximum amount of gold he can collect.
*/



#include<bits/stdc++.h>
using namespace std;


int main()
{
  int t;
  cin>>t;
  while(t--)
  {

    int n,m;
    cin>>n>>m;

    int arr[n][m];
    for(int i = 0 ; i < n ; i++)
     {
         for(int j = 0 ; j < m ; j++)
          {
                cin>>arr[i][j];
          }
     }

     for(int j = m-2; j>=0; j--)
     {

         for(int i = 0 ; i < n ; i++)
         {

             if(i==0&&i==n-1)
             arr[i][j] += arr[i][j+1];

            else if(i==0)
             arr[i][j] += max(arr[i][j+1],arr[i+1][j+1]);

             else if(i==n-1)
             arr[i][j] += max(arr[i][j+1],arr[i-1][j+1]);

             else
             arr[i][j] += max(arr[i+1][j+1],max(arr[i][j+1],arr[i-1][j+1]));


         }
     }

     int maxi = INT_MIN;
      for(int i = 0 ; i < n ; i++)
     {
         if(maxi<arr[i][0])
         maxi = arr[i][0];
     }

    cout<<maxi<<endl;
}
}
